﻿using System;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlServerCe;
using Common;
using Common.Entities;

namespace OrderSales.Model
{
    public class OrderModel
    {
        //private Order order;

        //public string DateOrder
        //{
        //    set
        //    {
        //        order.DateOrder = value;
        //        try
        //        {
        //            using (SqlCeConnection connection = WorkDataBase.CreateConnection())
        //            {
        //                if (connection.State != ConnectionState.Open)
        //                    connection.Open();
        //                string sqlQuery =
        //                    "update orders set DateOrder = @DateOrder where idOrder = @IdOrder";
        //                using (SqlCeCommand com = new SqlCeCommand(sqlQuery, connection))
        //                {
        //                    com.Parameters.Add("@IdOrder", this.order.IdOrder);
        //                    com.Parameters.Add("@DateOrder", this.order.DateOrder);
        //                    com.ExecuteNonQuery();
        //                }
        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            throw ex;
        //        }
        //    }
        //    get
        //    {
        //        return order.DateOrder;
        //    }
        //}

        //public string NumberOrder
        //{
        //    set
        //    {
        //        try
        //        {
        //            order.NumberOrder = value;
        //            using (SqlCeConnection connection = WorkDataBase.CreateConnection())
        //            {
        //                if (connection.State != ConnectionState.Open)
        //                    connection.Open();
        //                string sqlQuery =
        //                    "update orders set NumberOrder = @NumberOrder where idOrder = @IdOrder";
        //                using (SqlCeCommand com = new SqlCeCommand(sqlQuery, connection))
        //                {
        //                    com.Parameters.Add("@IdOrder", this.order.IdOrder);
        //                    com.Parameters.Add("@NumberOrder", this.order.NumberOrder);
        //                    com.ExecuteNonQuery();
        //                }
        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            throw ex;
        //        }
        //    }
        //    get
        //    {
        //        return order.NumberOrder;
        //    }
        //}

        //public List<OrderRecord> RecordOrders
        //{
        //    get
        //    {
        //        LoadRecordOrder();
        //        return order.RecordOrders;
        //    }
        //}

        //public double Amount
        //{
        //    get { return order.SumOrder; }
        //}

        //public OrderModel(Order order)
        //{
        //    this.order = order;
        //    LoadRecordOrder();
        //}

        //private void LoadRecordOrder()
        //{
        //    try
        //    {
        //        order.RecordOrders = new List<RecordOrder>();
        //        using (SqlCeConnection connection = WorkDataBase.CreateConnection())
        //        {
        //            if (connection.State != ConnectionState.Open)
        //                connection.Open();
        //            string sqlQuery =
        //                "SELECT SalesGoods.IdSaleGood, SalesGoods.Quantity, SalesGoods.Price, Goods.Name, Goods.IdGood " +
        //                "FROM SalesGoods INNER JOIN Goods ON SalesGoods.GoodId = Goods.IdGood " +
        //                "Where OrderId = @OrderId";
        //            using (SqlCeCommand com = new SqlCeCommand(sqlQuery, connection))
        //            {
        //                com.Parameters.Add("@OrderId", this.order.IdOrder);

        //                using (SqlCeDataReader dr = com.ExecuteReader())
        //                {
        //                    int idSales = dr.GetOrdinal("IdSaleGood");
        //                    int quantity = dr.GetOrdinal("Quantity");
        //                    int price = dr.GetOrdinal("Price");
        //                    int goodId = dr.GetOrdinal("IdGood");
        //                    int name = dr.GetOrdinal("Name");
        //                    while (dr.Read())
        //                    {
        //                        RecordOrder recordOrder = new RecordOrder();
        //                        recordOrder.IdRecordOrder = dr.GetInt32(idSales);
        //                        recordOrder.Price = dr.GetDouble(price);
        //                        recordOrder.Quantity = dr.GetDouble(quantity);
        //                        recordOrder.Amount = recordOrder.Quantity*recordOrder.Price;
        //                        recordOrder.GoodName = dr.GetString(name);
        //                        recordOrder.GoodId = dr.GetInt32(goodId);
        //                        this.order.RecordOrders.Add(recordOrder);
        //                    }
        //                }
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    double sum = 0;
        //    foreach (RecordOrder record in order.RecordOrders)
        //    {
        //        sum += record.Amount;
        //    }
        //    order.SumOrder = sum;
        //    if (sum > 0)
        //    {
        //        SaveAmount();
        //    }
        //}

        //private void SaveAmount()
        //{
        //    try
        //    {
        //        using (SqlCeConnection connection = WorkDataBase.CreateConnection())
        //        {
        //            if (connection.State != ConnectionState.Open)
        //                connection.Open();
        //            string sqlQuery =
        //                "update orders set SumOrder = @SumOrder where idOrder = @IdOrder";
        //            using (SqlCeCommand com = new SqlCeCommand(sqlQuery, connection))
        //            {
        //                com.Parameters.Add("@IdOrder", this.order.IdOrder);
        //                com.Parameters.Add("@SumOrder", this.order.SumOrder);
        //                com.ExecuteNonQuery();
        //            }
        //        }
        //    }
        //    catch(Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

        //public void AddRecordOrder(OrderRecord rec)
        //{
        //    try
        //    {
        //        using (SqlCeConnection connection = WorkDataBase.CreateConnection())
        //        {
        //            if (connection.State != ConnectionState.Open)
        //                connection.Open();
        //            string sqlQuery =
        //                "insert into SalesGoods (OrderId, GoodId, Price, Quantity) values (@OrderId, @GoodId, @Price, @Quantity)";
        //            using (SqlCeCommand com = new SqlCeCommand(sqlQuery, connection))
        //            {
        //                com.Parameters.Add("@OrderId", this.order.IdOrder);
        //                com.Parameters.Add("@GoodId", rec.GoodId);
        //                com.Parameters.Add("@Price", rec.Price);
        //                com.Parameters.Add("@Quantity", rec.Quantity);
        //                com.ExecuteNonQuery();
        //            }
        //        }
        //    }
        //    catch(Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

        //public void RemoveRecordOrder(OrderRecord rec)
        //{
        //    try
        //    {
        //        using (SqlCeConnection connection = WorkDataBase.CreateConnection())
        //        {
        //            if (connection.State != ConnectionState.Open)
        //                connection.Open();
        //            string sqlQuery =
        //                "delete from SalesGoods where IdSaleGood = @IdSalesGood";
        //            using (SqlCeCommand com = new SqlCeCommand(sqlQuery, connection))
        //            {

        //                com.Parameters.Add("@IdSalesGood", rec.IdRecordOrder);
        //                com.ExecuteNonQuery();
        //            }
        //        }
        //    }
        //    catch(Exception ex)
        //    {
        //        throw ex;
        //    }
        //}
    }
}
